package com.baizhi.controller;

import cn.hutool.captcha.ICaptcha;
import com.baizhi.entity.Admin;
import com.baizhi.service.AdminService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("admin")
public class AdminController {

    @Autowired

    private AdminService adminService;

    @RequestMapping("login")
    public String getAdminByName(String username, String password, String code) {
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        ICaptcha captcha = (ICaptcha) session.getAttribute("captcha");
        if (!captcha.verify(code)) return "login";
        try {
            subject.login(token);
            return "redirect:/menu/getAll";
        } catch (Exception e) {
            return "login";
        }
    }

    @RequestMapping("getAll")
    @ResponseBody
    public Map getAllAdmin() {
        HashMap map = new HashMap();
        try {
            map.put("data", adminService.findAll());
            map.put("code", 0);
            System.out.println();
            return map;
        } catch (RuntimeException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @ResponseBody
    @RequestMapping("getOne")
    public Admin getAdminById(Admin admin) {
        try {
            Admin ad = adminService.findAdminByField(admin);
            return ad;
        } catch (RuntimeException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @ResponseBody
    @RequestMapping("delById")
    public Boolean deleteAdmin(Admin admin) {
        try {
            adminService.removeAdminById(admin.getAdminId());
            return true;
        } catch (RuntimeException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @RequestMapping("delBulk")
    @ResponseBody
    public Boolean deleteMultiple(Integer[] ids) {
        try {
            adminService.removeMultiple(ids);
            return true;
        } catch (RuntimeException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @RequestMapping("update")
    @ResponseBody
    public Boolean updateAdmin(Admin admin) {
        try {
            adminService.renovateAdmin(admin);
            return true;
        } catch (RuntimeException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }


    @ResponseBody
    @RequestMapping("register")
    public Boolean addAdmin(Admin admin) {
        try {
            adminService.addAdmin(admin);
            return true;
        } catch (RuntimeException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @RequestMapping("loginOut")
    public String loginOut() {
        try {
            Session session = SecurityUtils.getSubject().getSession();
            session.stop();
            return "login";
        } catch (RuntimeException e) {
            e.printStackTrace();
            //因为需要返回一个view,所以全局异常里面应返回一个view.
           return "error";
        }
    }
}
